package com.zhongjian.technologycal.mall.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.zhongjian.technologycal.mall.R;
import com.zhongjian.technologycal.mall.bean.LogisticsBean;

import java.util.List;

/**
 * Created by wei on 2018/1/17.
 */

public class LogisticsInfoAdaper extends BaseAdapter {
    private Context context;
    private List<LogisticsBean> listItems;
    private LayoutInflater listContainer;

    public LogisticsInfoAdaper(Context context, List<LogisticsBean> listItems) {
        this.context = context;
        listContainer = LayoutInflater.from(context);
        this.listItems = listItems;
    }

    @Override
    public int getCount() {
        return listItems.size();
    }

    @Override
    public Object getItem(int position) {
        return listItems.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ListItemView listItemView = null;
        if (convertView == null) {
            listItemView = new ListItemView();
            convertView = listContainer.inflate(R.layout.items_logistics_info,
                    null);
            listItemView.logistcs_tv_time = (TextView) convertView
                    .findViewById(R.id.logistcs_tv_time);
            listItemView.logistcs_tv_state = (TextView) convertView
                    .findViewById(R.id.logistcs_tv_state);
            listItemView.logistcs_tv_acceptstation = (TextView) convertView
                    .findViewById(R.id.logistcs_tv_acceptstation);
            listItemView.logistcs_tv_date = convertView.findViewById(R.id.logistcs_tv_date);
            convertView.setTag(listItemView);
        } else {
            listItemView = (ListItemView) convertView.getTag();
        }
        //将数据填充到具体的view中
        if (!TextUtils.isEmpty(listItems.get(position).getData().getTraces().get(position).getAcceptTime())) {
            String str = listItems.get(position).getData().getTraces().get(position).getAcceptTime();
            String date = str.substring(0, 10);
            String time = str.substring(10, str.length());
            listItemView.logistcs_tv_date.setText(date);
            listItemView.logistcs_tv_time.setText(time);
        }
        listItemView.logistcs_tv_state.setVisibility(View.GONE);
        listItemView.logistcs_tv_state.setText("运输中");
        listItemView.logistcs_tv_acceptstation.setText("" + listItems.get(position).getData().getTraces().get(position).getAcceptStation());
        return convertView;
    }

    public final class ListItemView {
        private TextView logistcs_tv_time;
        private TextView logistcs_tv_state;
        private TextView logistcs_tv_acceptstation;
        private TextView logistcs_tv_date;
    }

}
